// index.js

var util = require('../../utils/util');
Page({
  data: {
    userInfo: {},
    appInstance: "",
    eventChannel: ""
  },
  onLoad(options) {
    var appInstance = getApp();
    this.data.appInstance = appInstance;
    this.data.eventChannel = this.getOpenerEventChannel();
    this.data.eventChannel.on('infoData', (infoData) => {
      this.setData({
        userInfo: infoData.data
      });
      console.log(this.data.userInfo)
    })
  },
  onReady() {

  },
  updateUsername() {
    var username = this.data.userInfo.username;
    var obj = {
      title: "名字",
      length: 10,
      data: username,
      key: "username"
    }
    wx.navigateTo({
      url: '/pages/inputinfo/inputinfo',
      events: {
        backShow: (data) => {
          console.log(data)
          this.setData({
            'userInfo.username': data.data.username
          })
          this.data.eventChannel.emit('backShow', {
            data: data.data
          })
        }
      },
      success: (res) => {
        res.eventChannel.emit('infoData', {
          data: obj
        })
      },
      fail: function (error) {
        console.log(error)
      }
    })
  },
  updatePhoneNumber() {
    var phoneNumber = this.data.userInfo.phoneNumber;
    var obj = {
      title: "手机号码",
      length: 12,
      data: phoneNumber,
      key: "phoneNumber"
    }
    wx.navigateTo({
      url: '/pages/inputinfo/inputinfo',
      events: {
        backShow: (data) => {
          console.log(data)
          this.setData({
            'userInfo.phoneNumber': data.data.phoneNumber
          })
          this.data.eventChannel.emit('backShow', {
            data: data.data
          })
        }
      },
      success: (res) => {
        res.eventChannel.emit('infoData', {
          data: obj
        })
      },
      fail: function (error) {
        console.log(error)
      }
    })
  },
  updateSign() {
    var sign = this.data.userInfo.sign;
    var obj = {
      title: "个性签名",
      length: 30,
      data: sign,
      key: "sign"
    }
    wx.navigateTo({
      url: '/pages/inputinfo/inputinfo',
      events: {
        backShow: (data) => {
          console.log(data)
          this.setData({
            'userInfo.sign': data.data.sign
          })
          this.data.eventChannel.emit('backShow', {
            data: data.data
          })
        }
      },
      success: (res) => {
        res.eventChannel.emit('infoData', {
          data: obj
        })
      },
      fail: function (error) {
        console.log(error)
      }
    })
  },
  updateuserStatus() {
    var flag = this.data.userInfo.flag;
    if (!flag) {
      return;
    }
    var userStatusStr = this.data.userInfo.userStatusStr;
    wx.navigateTo({
      url: '/pages/status/status',
      events: {
        backShow: (data) => {
          this.setData({
            'userInfo.userStatusStr': data.data.userStatusStr
          })
          this.data.eventChannel.emit('backShow', {
            data: data.data
          })
        }
      },
      success: function (res) {
        res.eventChannel.emit('infoData', {
          data: userStatusStr
        })
      },
      fail: function (error) {
        console.log(error)
      }
    })
  },
  updateUserProfile() {
    var that = this;
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        console.log(res.tempFiles[0].tempFilePath)
        var size = res.tempFiles[0].size;
        if (size / 1048576 > 8) {
          wx.showToast({
            title: '最大不超过8M',
            icon: 'error',
            duration: 2000
          })
          return;
        }
        wx.cropImage({
          src: res.tempFiles[0].tempFilePath, // 图片路径
          cropScale: '1:1', // 裁剪比例
          success(res) {
            //todo 这里先上传到后台
            var tempFilePath = res.tempFilePath;
            var url = '/applet/uploadProfileImage';
            util.uploadFile(url,
              tempFilePath, {
                userId: wx.getStorageSync('userInfo').id
              },
              (data) => {
                var jsonData = JSON.parse(data);
                console.log(jsonData)
                if (jsonData.code === 2000) {
                  var userInfo = wx.getStorageSync('userInfo');
                  userInfo.profileImagePath = util.backServerUrl +
                    jsonData.data.profileImagePath;
                  wx.setStorageSync('userInfo', userInfo)
                  that.data.eventChannel.emit('backShow', {
                    data: userInfo
                  })
                  that.setData({
                    userInfo: userInfo
                  })
                } else {
                  wx.showToast({
                    title: '修改失败',
                    icon: 'error',
                    duration: 2000
                  })
                }
              },
              (error) => {})
          }
        })
      },
      fail(error) {

      }
    })
  },
  switchChange(event) {
    var value = event.detail.value;
    var switchValue;
    if (value) {
      switchValue = 1;
    } else {
      switchValue = 0;

    }
    var url = '/applet/updateUserInfo';
    util.postRequest(
      url, {
        id: this.data.userInfo.id,
        notificationSwitch: this.data.userInfo.notificationSwitch
      },
      (data) => {
        if (data.code == 2000) {
          wx.showToast({
            title: '修改成功',
            icon: 'sucess',
            duration: 2000
          })
          this.setData({
            'userInfo.notificationSwitch': switchValue
          });
        } else {
          this.setData({
            'userInfo.notificationSwitch': this.data.userInfo.notificationSwitch
          });
          wx.showToast({
            title: '修改失败',
            icon: 'error',
            duration: 2000
          })
        }
      },
      (error) => {
        this.setData({
          'userInfo.notificationSwitch': this.data.userInfo.notificationSwitch
        });
        wx.showToast({
          title: '修改失败',
          icon: 'error',
          duration: 2000
        })
      });
  }
})